package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {
    @Resource
    private DeptService deptService;
    @Resource
    private EmpExprService empExprService;

    @Override
    public PageResult<Emp> pageEmp(EmpQueryParam param) {
        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());
        //排序
        page.addOrder(OrderItem.desc("create_time"));

         page = lambdaQuery().like(param.getName() != null && !param.getName().isEmpty(), Emp::getName, param.getName())
                .eq(param.getGender() != null, Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null, Emp::getEntryDate, param.getBegin(), param.getEnd())
                .page(page);
//        page= page(page);
        List<Emp> emps = page.getRecords();
        //拿到部门id
        List<Integer> deptIds = emps.stream().map(Emp::getDeptId).toList();
        //查询部门
        if (!deptIds.isEmpty()){
            List<Dept> depts = deptService.listByIds(deptIds);
            emps.forEach(emp -> {
                depts.forEach(dept -> {
                    if (emp.getDeptId()!=null && emp.getDeptId().equals(dept.getId())){
                        emp.setDeptName(dept.getName());
                    }
                });
            });
        }

        return new PageResult<>(page.getTotal(),page.getRecords());
    }

    /**
     * 新增员工
     * @param emp
     */
    @Override
    public void saveEmp(Emp emp) {
        //新增员工信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        save(emp);
        //新增员工经历信息
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(expr->{
            expr.setEmpId(emp.getId());
        });
        empExprService.saveBatch(exprList);
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @Override
    public Emp getEmpById(Integer id) {
        //查询员工
        Emp emp = getById(id);
        //查询员工经历
        LambdaQueryWrapper<EmpExpr> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(EmpExpr::getEmpId,id);
        List<EmpExpr> list = empExprService.list(wrapper);
        emp.setExprList(list);
        return emp;
    }
    /**
     * 修改员工信息
     * @param emp
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEmp(Emp emp) {
        //修改员工信息
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
        //删除员工经历
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId,emp.getId()));
        //新增员工经历
        List<EmpExpr> exprList = emp.getExprList();
        if (!exprList.isEmpty()) {
            exprList.forEach(expr->{
                expr.setEmpId(emp.getId());
                empExprService.saveBatch(exprList);
            });

        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeEmpById(List<Integer> ids) {
        removeBatchByIds(ids);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId,ids));
    }
}
